/// 充值记录数据模型
class PaymentRecord {
  final String orderId;
  final double amount;
  final int points;
  final String orderTitle;
  final int paymentStatus;
  final String paymentPlatform;
  final String createdAt;
  final String? paymentTime;

  PaymentRecord({
    required this.orderId,
    required this.amount,
    required this.points,
    required this.orderTitle,
    required this.paymentStatus,
    required this.paymentPlatform,
    required this.createdAt,
    this.paymentTime,
  });

  /// 从JSON创建实例
  factory PaymentRecord.fromJson(Map<String, dynamic> json) {
    return PaymentRecord(
      orderId: json['order_id'] ?? '',
      amount: (json['amount'] ?? 0.0).toDouble(),
      points: json['points'] ?? 0,
      orderTitle: json['order_title'] ?? '',
      paymentStatus: json['payment_status'] ?? 0,
      paymentPlatform: json['payment_platform'] ?? '',
      createdAt: json['created_at'] ?? '',
      paymentTime: json['payment_time'],
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'order_id': orderId,
      'amount': amount,
      'points': points,
      'order_title': orderTitle,
      'payment_status': paymentStatus,
      'payment_platform': paymentPlatform,
      'created_at': createdAt,
      'payment_time': paymentTime,
    };
  }

  /// 获取支付状态文本
  String get paymentStatusText {
    switch (paymentStatus) {
      case 0:
        return '待支付';
      case 1:
        return '已支付';
      case 2:
        return '支付失败';
      case 3:
        return '已退款';
      case 4:
        return '已关闭';
      default:
        return '未知状态';
    }
  }

  /// 获取支付平台文本
  String get paymentPlatformText {
    switch (paymentPlatform.toLowerCase()) {
      case 'alipay':
        return '支付宝';
      case 'wechat':
        return '微信';
      default:
        return paymentPlatform;
    }
  }

  /// 格式化积分显示
  String get formattedPoints {
    return points > 0 ? '+${points}积分' : '${points}积分';
  }

  /// 格式化金额显示
  String get formattedAmount {
    return '¥${amount.toStringAsFixed(2)}';
  }

  /// 格式化日期显示
  String get formattedDate {
    try {
      final date = DateTime.parse(createdAt);
      return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')} ${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}:${date.second.toString().padLeft(2, '0')}';
    } catch (e) {
      return createdAt;
    }
  }

  @override
  String toString() {
    return 'PaymentRecord(orderId: $orderId, amount: $amount, points: $points, paymentStatus: $paymentStatus)';
  }
}

/// 充值记录响应模型
class PaymentRecordResponse {
  final String status;
  final String message;
  final int total;
  final int page;
  final int pageSize;
  final List<PaymentRecord> records;

  PaymentRecordResponse({
    required this.status,
    required this.message,
    required this.total,
    required this.page,
    required this.pageSize,
    required this.records,
  });

  /// 从JSON创建实例
  factory PaymentRecordResponse.fromJson(Map<String, dynamic> json) {
    final dataMap = json['data'] as Map<String, dynamic>? ?? {};
    final recordsJson = dataMap['records'] as List<dynamic>? ?? [];
    
    return PaymentRecordResponse(
      status: json['status'] ?? 'error',
      message: json['message'] ?? '未知错误',
      total: dataMap['total'] ?? 0,
      page: dataMap['page'] ?? 1,
      pageSize: dataMap['page_size'] ?? 10,
      records: recordsJson.map((json) => PaymentRecord.fromJson(json)).toList(),
    );
  }

  /// 是否成功
  bool get isSuccess => status == 'success';

  /// 错误信息
  String get errorMessage => isSuccess ? '' : message;

  @override
  String toString() {
    return 'PaymentRecordResponse(status: $status, total: $total, records: ${records.length})';
  }
}
